package br.com.decorando.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import br.com.decorando.dao.CartaDAO;
import br.com.decorando.model.Carta;
import br.com.decorando.model.Perfil;
import br.comdecorando.adapter.CartaAdapter;
import br.comdecorando.adapter.PerfilAdapter;
import br.comdecorando.adapter.CartaAdapter.CartaHolder;
import br.comdecorando.adapter.PerfilAdapter.PerfilHolder;


import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

public class AdicionaCartaActivity extends Activity {
	CartaAdapter adapter;
	int idAssunto = 0;
	CartaDAO cartaDAO = new CartaDAO(this);
	List<Carta> cartas = new ArrayList<Carta>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_adiciona_carta);
		Intent intent = getIntent();
		idAssunto = intent.getIntExtra("idAssunto", 1);
		listar(idAssunto);
	}
	
	public void excluir(View view){
		final Carta carta = ((CartaHolder) view.getTag()).getCarta();
		new AlertDialog.Builder(this)
        .setMessage(getResourceString(R.string.excluir_carta, carta.getPergunta()))
        .setCancelable(false)
        .setPositiveButton(getResourceString(R.string.yes), 
        new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int id) {
                  excluirCarta(carta);
             }
         })
        .setNegativeButton(getResourceString(R.string.no), null)
        .show();		
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.adiciona_carta, menu);
		return true;
	}

	public void listar(int idAssunto){
		ListView listView = (ListView) findViewById(R.id.listCartas);
		cartas = cartaDAO.listarGeral(idAssunto);
		adapter = new CartaAdapter(this, R.layout.list_cartas, cartas);		
		listView.setAdapter(adapter);		
	}
	
	public void cadastrar(View view){		
		EditText edtPergunta = (EditText) findViewById(R.id.edtPergunta);		
		EditText edtResposta = (EditText) findViewById(R.id.edtResposta);		
		Carta carta = new Carta();
		carta.setPergunta(edtPergunta.getText().toString());
		carta.setResposta(edtResposta.getText().toString());
		Date agora = new Date();
		carta.setDataCriacao(agora);
		carta.setDataUltimaRevisao(agora);
		carta.setDificuldade(5);
		carta.setIdAssunto(idAssunto);	
		if(carta.getPergunta() == null || carta.getPergunta().trim().equals("") || 
				carta.getResposta() == null || carta.getResposta().trim().equals("")){
			exibirMensagem(getResourceString(R.string.error_carta_vazio), getResourceString(R.string.error_carta_vazio));			
		} else {
			if(!adapter.isEdit()){					
				cartaDAO.save(carta);
				listar(idAssunto);
				setupEditCampos("", "");
			} else {
				alterarCarta(carta);
			}
		}
	}
	
	public void alterarCarta(Carta carta){
		adapter.getEditCarta().setPergunta(carta.getPergunta());
		adapter.getEditCarta().setResposta(carta.getResposta());
		if (carta.getDataUltimaRevisao() != null) {
			adapter.getEditCarta().setDataUltimaRevisao(carta.getDataUltimaRevisao());
		}
		cartaDAO.update(adapter.getEditCarta());		
		endEdit();		
		listar(idAssunto);
	}
	
	public void editar(View view){
		endEdit();
		CartaHolder holder = (CartaHolder) view.getTag();		
		setupButton(R.drawable.ic_action_edit);
		setupEditCampos(holder.getPergunta(), holder.getResposta());
		adapter.setEditHolder(holder);
	}
	
	public void excluirCarta(Carta carta){
		cartaDAO.delete(carta);
		listar(idAssunto);
	}
	
	public void endEdit(){		
		setupButton(R.drawable.ic_action_new);
		setupEditCampos("", "");			
		adapter.setEditHolder(null);					
	}
	
	public void setupButton(int image){
		ImageButton button = (ImageButton) findViewById(R.id.btnAdicionarCarta);
		button.setBackgroundResource(image);
	}
			
	public void setupEditCampos(String pergunta, String resposta){
		EditText edtPergunta = (EditText) findViewById(R.id.edtPergunta);		
		EditText edtResposta = (EditText) findViewById(R.id.edtResposta);
		edtPergunta.setText(pergunta);
		edtResposta.setText(resposta);
	}
	
	public void cancelar(View view){									
		endEdit();
	}
	
	
	public void exibirMensagem(String title, String message){
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle(title);
		alert.setMessage(message);
		alert.setPositiveButton("OK", null);
		alert.show();
	}
	
	public String getResourceString(int string, Object... args){
		return getResources().getString(string, args); 
	}	
	
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
    	Intent intent;
    	switch (item.getItemId()) {
			case R.id.menu_perfil:
				intent = new Intent(this, PerfilActivity.class);
				startActivity(intent);
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
    }
}
